package com.hzhr.iot.cqgas.ds.service;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneBumenDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.EneCsrDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrDeptDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrUserDao;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneBumenEntity;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneBumenExample;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneCsrEntity;
import com.hzhr.iot.cqgas.ds.entity.smartmeter.EneCsrExample;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptExample;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UsrUserDao usrUserDao;
    @Autowired
    EneCsrDao eneCsrDao;
    @Autowired
    UsrDeptDao usrDeptDao;
    @Autowired
    EneBumenDao eneBumenDao;

    @Value("${companyId}")
    String companyId;

    private Logger logger = LoggerFactory.getLogger(UserService.class);

    public void syncUser() {
        int addCount = 0;
        int updateCount = 0;
        long start = new Date().getTime();

        EneCsrExample eneCsrExample = new EneCsrExample();
        eneCsrExample.createCriteria();
        List<EneCsrEntity> eneCsrEntities = eneCsrDao.selectByExample(eneCsrExample);
        for (EneCsrEntity eneCsrEntity :
                eneCsrEntities) {
            try {
                String csrId = eneCsrEntity.getCsrid();
                String csrYgId = eneCsrEntity.getCsrygid();
                if (csrYgId == null) continue;
                String csrName = HzhrTypeUtil.object2string(eneCsrEntity.getCsrname());
                String password = eneCsrEntity.getCsrpwd();
                if (password == null) continue;
                String csrBmId = eneCsrEntity.getCsrbmId();
                if (csrBmId == null) continue;
                EneBumenEntity eneBumenEntity = eneBumenDao.selectByPrimaryKey(csrBmId);
                String orgId = eneBumenEntity.getOrgId();
                String csrMobile = eneCsrEntity.getCsrmobile();
                orgId = orgId.replaceFirst("0000", "01000901");

                //新增或更新部门(所有上级部门置位0)
                UsrUserExample usrUserExample = new UsrUserExample();
                usrUserExample.createCriteria().andOutIdEqualTo(csrId);
                List<UsrUserEntity> usrUserEntities = usrUserDao.selectByExample(usrUserExample);
                if (usrUserEntities.size() > 0) {
                    UsrUserEntity usrUserEntity = usrUserEntities.get(0);
                    usrUserEntity.setOutId(csrId);
                    usrUserEntity.setUserName(csrYgId);
                    usrUserEntity.setPhone(HzhrTypeUtil.string2long(csrMobile));
                    usrUserEntity.setRealName(csrName);
                    usrUserEntity.setPassword(password);
                    usrUserEntity.setCompanyId(Integer.parseInt(companyId));

                    UsrDeptExample usrDeptExample = new UsrDeptExample();
                    usrDeptExample.createCriteria().andOutIdEqualTo(csrBmId);
                    List<UsrDeptEntity> usrDeptEntities = usrDeptDao.selectByExample(usrDeptExample);
                    if (usrDeptEntities.size() > 0) {
                        usrUserEntity.setDeptId(usrDeptEntities.get(0).getId());
                    } else {
                        continue;
                    }

                    usrUserEntity.setIsNewUser(CommonOptions.YES_NO.NO);
                    usrUserEntity.setUserCode(orgId);
                    usrUserEntity.setUpdateTime(new Date());
                    usrUserEntity.setUpdateUserId(0);
                    usrUserEntity.setResourceCode(orgId);
                    usrUserEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrUserEntity.setState("1");
                    usrUserEntity.setType("2");
                    usrUserDao.updateByPrimaryKey(usrUserEntity);
                    updateCount++;
                } else {
                    UsrUserEntity usrUserEntity = new UsrUserEntity();
                    usrUserEntity.setOutId(csrId);
                    usrUserEntity.setUserName(csrYgId);
                    usrUserEntity.setPhone(HzhrTypeUtil.string2long(csrMobile));
                    usrUserEntity.setRealName(csrName);
                    usrUserEntity.setPassword(password);
                    usrUserEntity.setCompanyId(Integer.parseInt(companyId));

                    UsrDeptExample usrDeptExample = new UsrDeptExample();
                    usrDeptExample.createCriteria().andOutIdEqualTo(csrBmId);
                    List<UsrDeptEntity> usrDeptEntities = usrDeptDao.selectByExample(usrDeptExample);
                    if (usrDeptEntities.size() > 0) {
                        usrUserEntity.setDeptId(usrDeptEntities.get(0).getId());
                    } else {
                        continue;
                    }

                    usrUserEntity.setIsNewUser(CommonOptions.YES_NO.NO);
                    usrUserEntity.setUserCode(orgId);
                    usrUserEntity.setCreateTime(new Date());
                    usrUserEntity.setCreateUserId(0);
                    usrUserEntity.setResourceCode(orgId);
                    usrUserEntity.setIsValid(CommonOptions.YES_NO.YES);
                    usrUserEntity.setState("1");
                    usrUserEntity.setType("2");
                    usrUserDao.insert(usrUserEntity);
                    addCount++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        long end = new Date().getTime();
        logger.info("同步用户结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("更新数量: " + updateCount + "条");
        logger.info("新增数量: " + addCount + "条");

    }
}
